import xp from './lib/algorithm.js';
//暂时使用element ui
import {
  ElMessage,
} from 'element-plus';


const getLongLatArr = function (obj) {
  const {
    lng,
    lat
  } = obj;
  return [lng, lat];
}
/* 直角箭头 */
const StraightArrow = class {
  constructor(basicCesium, color) {
    this.basicCesium = basicCesium
    this.viewer = basicCesium.viewer;

    this.type = 'StraightArrow';
    this.objId = basicCesium.guid();
    this.color = color ? color : '#a64968'

    this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.canvas);
    this.material = Cesium.Color.fromCssColorString(this.color).withAlpha(0.8);
    this.outlineMaterial = new Cesium.PolylineDashMaterialProperty({
      dashLength: 16,
      color: Cesium.Color.fromCssColorString('#0000FF').withAlpha(0.8)
    });
    this.positions = [];
    this.firstPoint = null;
    this.floatPoint = null;
    this.arrowEntity = null;
    //state用于区分当前的状态 0 为删除 1为添加 2为编辑 
    this.state = -1;
    this.selectPoint = null;
    this.modifyHandler = null;
  }

  disable() {
    this.positions = []
    if (this.firstPoint) {
      this.basicCesium.removeByEntity(this.firstPoint);
      this.firstPoint = null;
    }
    if (this.floatPoint) {
      this.basicCesium.removeByEntity(this.floatPoint);
      this.floatPoint = null;
    }
    if (this.arrowEntity) {
      this.basicCesium.removeByEntity(this.arrowEntity);
      this.arrowEntity = null;
    }
    if (this.handler) {
      this.handler.destroy();
      this.handler = null;
    }
    if (this.selectPoint) {
      this.selectPoint = null;
    }
    if (this.modifyHandler) {
      this.modifyHandler.destroy();
      this.modifyHandler = null;
    }
    this.state = -1;
  }
  //绘制箭头
  startDraw() {
    this.state = 1;
    this.handler.setInputAction((e) => {
      this.getMouseInfo(e.position, 'left_click');
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

    this.handler.setInputAction((e) => { //移动时绘制面
      this.getMouseInfo(e.endPosition, 'mouse_move');
      let pick = this.basicCesium.mousePicked(e.endPosition);
      if (pick == undefined) {
        if (this.state != -1) {
          this.changeTipsPosition(e.endPosition);
        } else {
          this.removeTips();
        }
      }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  }

  getMouseInfo(position, type) {
    let cartesian = this.basicCesium.xyToWorld(position);
    if (!cartesian) return false;
    if (type == 'left_click') {
      if (this.positions.length == 0) {
        this.floatPoint = this.basicCesium.entityBillboard(cartesian, {
          attr: {
            type: 'floatPoint',
            text: 'editPoint'
          }
        });
        this.firstPoint = this.basicCesium.entityBillboard(cartesian, {
          attr: {
            type: 'firstPoint',
            text: 'editPoint'
          }
        })
        this.positions.push(cartesian);
      }

      if (this.positions.length == 2) {
        this.handler.destroy();
        this.arrowEntity.objId = this.objId;
        this.state = -1;
        this.firstPoint.show = false;
        this.floatPoint.show = false;
        this.basicCesium.mouseStyle('default')
        this.removeTips();
      }
    } else if (type == 'mouse_move') {
      if (this.positions.length < 1) {
        return null;
      }
      this.addTips('左键单击完成绘制');
      this.floatPoint.position.setValue(cartesian);
      if (this.positions.length >= 1) {
        if (!Cesium.defined(this.arrowEntity)) {
          this.positions.push(cartesian);
          this.arrowEntity = this.showArrowPolygon(this.positions);
        } else {
          this.positions.pop();
          this.positions.push(cartesian);
        }
      }
    }
  }

  showArrowPolygon(positions) {
    let that = this;
    let update = function () {
      if (positions.length < 2) return null;
      let p1 = positions[0];
      let p2 = positions[1];
      let firstPoint = that.basicCesium.worldToLnglat(p1);
      let endPoint = that.basicCesium.worldToLnglat(p2);
      let arrow = [];
      let res = xp.algorithm.fineArrow(
        getLongLatArr(firstPoint),
        getLongLatArr(endPoint)
      );
      let index = JSON.stringify(res).indexOf("null");
      if (index != -1) return [];
      for (var i = 0; i < res.length; i++) {
        var c3 = new Cesium.Cartesian3(res[i].x, res[i].y, res[i].z);
        arrow.push(c3);
      }
      return new Cesium.PolygonHierarchy(arrow);
    }
    return this.basicCesium.activeEntityPolygon(update, {
      material: this.material,
      outlineMaterial: this.outlineMaterial
    })
  }

  //修改箭头
  startModify() {
    this.state = 2;
    this.firstPoint.show = true;
    this.floatPoint.show = true;
    let that = this;
    if (!this.modifyHandler) {
      this.modifyHandler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
    }
    this.modifyHandler.setInputAction(function (e) {
      let pick = that.basicCesium.mousePicked(e.position);
      if (Cesium.defined(pick) && pick.id) {
        let attr = pick.id.attr != undefined ? pick.id.attr : {};
        if (attr.text == 'editPoint') {
          that.selectPoint = pick.id;
          that.viewer.scene.screenSpaceCameraController.enableRotate = false;
        }
      }
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN);

    this.modifyHandler.setInputAction((e) => {
      this.viewer.scene.screenSpaceCameraController.enableRotate = true;
      this.selectPoint = null;
    }, Cesium.ScreenSpaceEventType.LEFT_UP);

    this.modifyHandler.setInputAction(function (e) {
      that.modifyMouseInfo(e.position, 'left_click');
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

    this.modifyHandler.setInputAction(function (e) { //移动时绘制面
      that.modifyMouseInfo(e.endPosition, 'mouse_move');
      that.changeTipsPosition(e.endPosition);
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  }

  modifyMouseInfo(position, type) {
    let cartesian = this.basicCesium.xyToWorld(position);
    if (!cartesian) return false;
    if (type == 'left_click') {
      let pick = this.basicCesium.mousePicked(position);
      if (!Cesium.defined(pick) || pick.id) {
        this.modifyHandler.destroy();
        this.modifyHandler = null;
        this.firstPoint.show = false;
        this.floatPoint.show = false;
        this.state = -1;
        this.selectPoint = null;
      }
    } else if (type == 'mouse_move') {
      if (this.selectPoint != null) {
        this.selectPoint.position.setValue(cartesian);
        let attr = this.selectPoint.attr != undefined ? this.selectPoint.attr : {};
        if (attr.type == "firstPoint") {
          this.positions[0] = cartesian;
        }
        if (attr.type == "floatPoint") {
          this.positions[1] = cartesian;
        }
      }
    }
  }
  //加载数据
  createByData(data) {
    this.positions = []; //控制点
    this.state = -1; //state用于区分当前的状态 0 为删除 1为添加 2为编辑 
    this.pointArr = []; //中间各点
    this.selectPoint = null;
    this.modifyHandler = null;
    this.positions = data;
    //构建控制点
    this.firstPoint = this.basicCesium.entityBillboard(this.positions[0], {
      attr: {
        type: 'firstPoint',
        text: 'editPoint'
      }
    });
    this.firstPoint.show = false;
    this.pointArr.push(this.firstPoint);
    this.floatPoint = this.basicCesium.entityBillboard(this.positions[1], {
      attr: {
        type: 'floatPoint',
        text: 'editPoint'
      }
    });
    this.floatPoint.show = false;
    this.pointArr.push(this.floatPoint);

    this.arrowEntity = this.showArrowPolygon(this.positions);
    this.arrowEntity.objId = this.objId;
  }
  //清除绘制箭头
  clear() {
    this.state = 0;
    if (this.firstPoint) this.basicCesium.removeByEntity(this.firstPoint);
    if (this.floatPoint) this.basicCesium.removeByEntity(this.floatPoint);
    if (this.arrowEntity) this.basicCesium.removeByEntity(this.arrowEntity);
    this.state = -1;
  }

  changeTipsPosition(position) {
    let div = document.getElementById('tips');
    if (div) {
      div.style.left = position.x + 15 + 'px';
      div.style.top = position.y + 'px';
      div.style.display = 'block';
    }
  }

  addTips(text = '', ) {
    let div = document.getElementById('tips');
    div.innerHTML = `<div>${text}</div>`
  }

  removeTips() {
    let div = document.getElementById('tips');
    if (div) {
      div.style.display = 'none'
      div.innerHTML = ''
    }
  }
}

/* 攻击箭头 */
const AttackArrow = class {
  constructor(basicCesium, color) {
    this.basicCesium = basicCesium
    this.viewer = basicCesium.viewer;

    this.type = 'AttackArrow';
    this.objId = basicCesium.guid();
    this.color = color ? color : '#a64968';
    this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.canvas);
    this.material = Cesium.Color.fromCssColorString(this.color).withAlpha(0.8);
    this.outlineMaterial = new Cesium.PolylineDashMaterialProperty({
      dashLength: 16,
      color: Cesium.Color.fromCssColorString('#0000FF').withAlpha(0.8)
    });
    this.positions = [];
    //state用于区分当前的状态 0 为删除 1为添加 2为编辑 
    this.state = -1;
    this.floatPoint = null;
    this.arrowEntity = null;
    this.pointArr = []; //中间各点
    this.selectPoint = null;
    this.modifyHandler = null;
  }

  disable() {
    this.positions = [];
    if (this.arrowEntity) {
      this.basicCesium.removeByEntity(this.arrowEntity);
      this.arrowEntity = null;
    }
    if (this.handler) {
      this.handler.destroy();
      this.handler = null;
    }
    if (this.selectPoint) {
      this.selectPoint = null;
    }
    for (var i = 0; i < this.pointArr.length; i++) {
      if (this.pointArr[i]) this.basicCesium.removeByEntity(this.pointArr[i]);
    }
    if (this.modifyHandler) {
      this.modifyHandler.destroy();
      this.modifyHandler = null;
    }
    this.state = -1;
  }

  startDraw() {
    this.state = 1;
    let that = this;
    this.handler.setInputAction(function (e) {
      that.getMouseInfo(e.position, 'left_click');
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

    this.handler.setInputAction(function (e) { //移动时绘制面
      that.getMouseInfo(e.endPosition, 'mouse_move');
      let pick = that.basicCesium.mousePicked(e.endPosition);
      if (pick == undefined) {
        if (that.state != -1) {
          that.changeTipsPosition(e.endPosition);
        } else {
          that.removeTips();
        }
      }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

    this.handler.setInputAction(function (e) {
      that.getMouseInfo(e.position, 'left_double_click');
    }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
  }

  getMouseInfo(position, type) {
    let cartesian = this.basicCesium.xyToWorld(position);
    if (!cartesian) return false;
    if (type == 'left_click') {
      if (this.positions.length == 0) {
        this.floatPoint = this.basicCesium.entityBillboard(cartesian);
      }
      this.positions.push(cartesian);
      let point = this.basicCesium.entityBillboard(cartesian, {
        attr: {
          type: 'controlPoint',
          text: 'editPoint'
        }
      });
      if (this.positions.length > 2) {
        point.wz = this.positions.length - 2
      } else {
        point.wz = this.positions.length - 1;
      }
      this.pointArr.push(point);
    } else if (type == 'mouse_move') {
      if (this.positions.length == 1) {
        this.addTips('左键单击继续绘制');
      }
      if (this.positions.length < 2) return;
      this.addTips('左键单击继续绘制，左键双击完成绘制');
      this.floatPoint.position.setValue(cartesian);
      if (!Cesium.defined(this.arrowEntity)) {
        this.positions.push(cartesian);
        this.arrowEntity = this.showArrowPolygon(this.positions);
      } else {
        this.positions.pop();
        this.positions.push(cartesian);
      }
    } else if (type == 'left_double_click') {
      this.pointArr.forEach(i => i.show = false);
      if (this.floatPoint) { //移除动态点
        this.floatPoint.show = false;
        this.basicCesium.removeByEntity(this.floatPoint);
        this.floatPoint = null;
      }
      this.positions.pop();
      this.basicCesium.removeByEntity(this.pointArr[this.pointArr.length - 1]);
      this.positions.splice(this.positions.length - 2, 1);
      console.log(this.positions)
      this.arrowEntity.objId = this.objId;
      this.handler.destroy();
      this.state = -1;
      this.basicCesium.mouseStyle('default')
      this.removeTips();
    }
  }

  showArrowPolygon(positions) {
    let that = this;
    let update = function () {
      if (positions.length < 3) {
        return null;
      }
      let longlatArr = [];
      for (let i = 0; i < positions.length; i++) {
        let longlat = that.basicCesium.worldToLnglat(positions[i]);
        longlatArr.push(getLongLatArr(longlat));
      }
      let res = xp.algorithm.tailedAttackArrow(longlatArr);
      let index = JSON.stringify(res.polygonalPoint).indexOf('null');
      let result = [];
      if (index == -1) result = res.polygonalPoint
      return new Cesium.PolygonHierarchy(result);
    }
    return this.basicCesium.activeEntityPolygon(update, {
      material: this.material,
      outlineMaterial: this.outlineMaterial
    })
  }

  //修改箭头
  startModify() {
    this.state = 2;
    this.pointArr.forEach(i => i.show = true)
    let that = this;
    if (!this.modifyHandler) {
      this.modifyHandler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
    }
    this.modifyHandler.setInputAction(function (e) {
      let pick = that.basicCesium.mousePicked(e.position);
      if (Cesium.defined(pick) && pick.id) {
        let attr = pick.id.attr != undefined ? pick.id.attr : {};
        if (attr.text == 'editPoint') {
          that.selectPoint = pick.id;
          that.viewer.scene.screenSpaceCameraController.enableRotate = false;
        }
      }
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN);

    this.modifyHandler.setInputAction((e) => {
      this.viewer.scene.screenSpaceCameraController.enableRotate = true;
      this.selectPoint = null;
    }, Cesium.ScreenSpaceEventType.LEFT_UP);

    this.modifyHandler.setInputAction(function (e) {
      that.modifyMouseInfo(e.position, 'left_click');
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

    this.modifyHandler.setInputAction(function (e) { //移动时绘制面
      that.modifyMouseInfo(e.endPosition, 'mouse_move');
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  }

  modifyMouseInfo(position, type) {
    let cartesian = this.basicCesium.xyToWorld(position);
    if (!cartesian) return false;
    if (type == 'left_click') {
      let pick = this.basicCesium.mousePicked(position);
      if (!Cesium.defined(pick) || pick.id) {
        this.modifyHandler.destroy();
        this.modifyHandler = null;
        this.pointArr.forEach(i => i.show = false);
        this.state = -1;
        this.selectPoint = null;
      }
    } else if (type == 'mouse_move') {
      if (this.selectPoint != null) {
        this.selectPoint.position.setValue(cartesian);
        let attr = this.selectPoint.attr != undefined ? this.selectPoint.attr : {};
        if (attr.type == "controlPoint" && this.selectPoint.wz != undefined) {
          this.positions[this.selectPoint.wz] = cartesian;
        }
      }
    }
  }
  //加载数据
  createByData(data) {
    this.positions = []; //控制点
    this.state = -1; //state用于区分当前的状态 0 为删除 1为添加 2为编辑 
    this.floatPoint = null;
    this.pointArr = []; //中间各点
    this.selectPoint = null;
    this.modifyHandler = null;
    this.positions = data;
    //构建控制点
    for (var i = 0; i < this.positions.length; i++) {
      var point = this.basicCesium.entityBillboard(this.positions[i], {
        attr: {
          type: 'controlPoint',
          text: 'editPoint'
        }
      });
      point.show = false;
      point.wz = i;
      this.pointArr.push(point);
    }
    this.arrowEntity = this.showArrowPolygon(this.positions);
    this.arrowEntity.objId = this.objId;
  }
  //清除绘制箭头
  clear() {
    this.state = 0;
    for (var i = 0; i < this.pointArr.length; i++) {
      if (this.pointArr[i]) this.basicCesium.removeByEntity(this.pointArr[i]);
    }
    if (this.arrowEntity) this.basicCesium.removeByEntity(this.arrowEntity);
    this.state = -1;
  }

  changeTipsPosition(position) {
    let div = document.getElementById('tips');
    if (div) {
      div.style.left = position.x + 15 + 'px';
      div.style.top = position.y + 'px';
      div.style.display = 'block';
    }
  }

  addTips(text = '', ) {
    let div = document.getElementById('tips');
    div.innerHTML = `<div>${text}</div>`
  }

  removeTips() {
    let div = document.getElementById('tips');
    if (div) {
      div.style.display = 'none'
      div.innerHTML = ''
    }
  }

}

const PincerArrow = class {
  constructor(basicCesium, color) {
    this.basicCesium = basicCesium
    this.viewer = basicCesium.viewer;

    this.type = 'PincerArrow';
    this.objId = basicCesium.guid();
    this.color = color ? color : '#a64968';
    this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.canvas);
    this.material = Cesium.Color.fromCssColorString(this.color).withAlpha(0.8);
    this.outlineMaterial = new Cesium.PolylineDashMaterialProperty({
      dashLength: 16,
      color: Cesium.Color.fromCssColorString('#0000FF').withAlpha(0.8)
    });
    this.positions = [];
    //state用于区分当前的状态 0 为删除 1为添加 2为编辑 
    this.state = -1;
    this.floatPoint = null;
    this.arrowEntity = null;
    this.pointArr = []; //中间各点
    this.selectPoint = null;
    this.modifyHandler = null;
  }

  disable() {
    this.positions = [];
    if (this.arrowEntity) {
      this.basicCesium.removeByEntity(this.arrowEntity);
      this.arrowEntity = null;
    }
    if (this.handler) {
      this.handler.destroy();
      this.handler = null;
    }
    if (this.selectPoint) {
      this.selectPoint = null;
    }
    for (var i = 0; i < this.pointArr.length; i++) {
      if (this.pointArr[i]) this.basicCesium.removeByEntity(this.pointArr[i]);
    }
    if (this.modifyHandler) {
      this.modifyHandler.destroy();
      this.modifyHandler = null;
    }
    this.state = -1;
  }

  startDraw() {
    this.state = 1;
    let that = this;
    this.handler.setInputAction(function (e) {
      that.getMouseInfo(e.position, 'left_click');
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

    this.handler.setInputAction(function (e) { //移动时绘制面
      that.getMouseInfo(e.endPosition, 'mouse_move');

      let pick = that.basicCesium.mousePicked(e.endPosition);
      if (pick == undefined) {
        if (that.state != -1) {
          that.changeTipsPosition(e.endPosition);
        } else {
          that.removeTips();
        }
      }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  }

  getMouseInfo(position, type) {
    let cartesian = this.basicCesium.xyToWorld(position);
    if (!cartesian) return false;
    if (type == 'left_click') {
      if (this.positions.length == 0) {
        this.floatPoint = this.basicCesium.entityBillboard(cartesian);
      }
      this.positions.push(cartesian);
      let point = this.basicCesium.entityBillboard(cartesian, {
        attr: {
          type: 'controlPoint',
          text: 'editPoint'
        }
      });
      if (this.positions.length > 2) {
        point.wz = this.positions.length - 2
      } else {
        point.wz = this.positions.length - 1;
      }
      this.pointArr.push(point);
      if (this.positions.length > 5) {
        for (var i = 0; i < this.pointArr.length; i++) {
          this.pointArr[i].show = false;
        }
        if (this.floatPoint) { //移除动态点
          this.floatPoint.show = false;
          this.basicCesium.removeByEntity(this.floatPoint);
          this.floatPoint = null;
        }
        this.positions.splice(this.positions.length - 2, 1);
        this.basicCesium.mouseStyle('default')
        this.handler.destroy();
        this.arrowEntity.objId = this.objId;
        this.removeTips();
        this.state = -1;
      }
    } else if (type == 'mouse_move') {
      if (this.positions.length >= 5) {
        this.addTips('左键单击完成绘制');
      } else if (this.positions.length == 1) {
        this.addTips('左键单击继续绘制');
      }
      if (this.positions.length < 2) return;
      this.floatPoint.position.setValue(cartesian);

      if (this.positions.length >= 2) {
        if (!Cesium.defined(this.arrowEntity)) {
          this.positions.push(cartesian);
          this.arrowEntity = this.showArrowPolygon(this.positions);
        } else {
          this.positions.pop();
          this.positions.push(cartesian);
        }
      }

    }
  }

  showArrowPolygon(positions) {
    let that = this;
    let update = function () {
      if (positions.length < 3) {
        return null;
      }
      let longlatArr = [];
      for (let i = 0; i < positions.length; i++) {
        let longlat = that.basicCesium.worldToLnglat(positions[i]);
        longlatArr.push(getLongLatArr(longlat));
      }
      let res = xp.algorithm.doubleArrow(longlatArr);
      let result = [];
      var index = JSON.stringify(res.polygonalPoint).indexOf("null");
      if (index == -1) result = res.polygonalPoint;
      return new Cesium.PolygonHierarchy(result);
    }
    return this.basicCesium.activeEntityPolygon(update, {
      material: this.material,
      outlineMaterial: this.outlineMaterial
    });
  }

  //修改箭头
  startModify() {
    this.state = 2;
    this.pointArr.forEach(i => i.show = true)
    let that = this;
    if (!this.modifyHandler) {
      this.modifyHandler = new Cesium.ScreenSpaceEventHandler(that.viewer.canvas);
    }
    this.modifyHandler.setInputAction(function (e) {
      let pick = that.basicCesium.mousePicked(e.position);
      if (Cesium.defined(pick) && pick.id) {
        let attr = pick.id.attr != undefined ? pick.id.attr : {};
        if (attr.text == 'editPoint') {
          that.selectPoint = pick.id;
          that.viewer.scene.screenSpaceCameraController.enableRotate = false;
        }
      }
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN);

    this.modifyHandler.setInputAction((e) => {
      this.viewer.scene.screenSpaceCameraController.enableRotate = true;
      this.selectPoint = null;
    }, Cesium.ScreenSpaceEventType.LEFT_UP);

    this.modifyHandler.setInputAction(function (e) {
      that.modifyMouseInfo(e.position, 'left_click');
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

    this.modifyHandler.setInputAction(function (e) { //移动时绘制面
      that.modifyMouseInfo(e.endPosition, 'mouse_move');
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  }

  modifyMouseInfo(position, type) {
    let cartesian = this.basicCesium.xyToWorld(position);
    if (!cartesian) return false;
    if (type == 'left_click') {
      let pick = this.basicCesium.mousePicked(position);
      if (!Cesium.defined(pick) || pick.id) {
        this.modifyHandler.destroy();
        this.modifyHandler = null;
        this.pointArr.forEach(i => i.show = false);
        this.state = -1;
        this.selectPoint = null;
      }
    } else if (type == 'mouse_move') {
      if (this.selectPoint != null) {
        this.selectPoint.position.setValue(cartesian);
        let attr = this.selectPoint.attr != undefined ? this.selectPoint.attr : {};
        if (attr.type == "controlPoint" && this.selectPoint.wz != undefined) {
          this.positions[this.selectPoint.wz] = cartesian;
        }
      }
    }
  }
  //加载数据
  createByData(data) {
    this.positions = []; //控制点
    this.state = -1; //state用于区分当前的状态 0 为删除 1为添加 2为编辑 
    this.floatPoint = null;
    this.pointArr = []; //中间各点
    this.selectPoint = null;
    this.modifyHandler = null;
    this.positions = data;
    //构建控制点
    for (var i = 0; i < this.positions.length; i++) {
      var point = this.basicCesium.entityBillboard(this.positions[i], {
        attr: {
          type: 'controlPoint',
          text: 'editPoint'
        }
      });
      point.show = false;
      point.wz = i;
      this.pointArr.push(point);
    }
    this.arrowEntity = this.showArrowPolygon(this.positions);
    this.arrowEntity.objId = this.objId;
  }
  //清除绘制箭头
  clear() {
    this.state = 0;
    for (var i = 0; i < this.pointArr.length; i++) {
      if (this.pointArr[i]) this.basicCesium.removeByEntity(this.pointArr[i]);
    }
    if (this.arrowEntity) this.basicCesium.removeByEntity(this.arrowEntity);
    this.state = -1;
  }

  changeTipsPosition(position) {
    let div = document.getElementById('tips');
    if (div) {
      div.style.left = position.x + 15 + 'px';
      div.style.top = position.y + 'px';
      div.style.display = 'block';
    }
  }

  addTips(text = '', ) {
    let div = document.getElementById('tips');
    div.innerHTML = `<div>${text}</div>`
  }

  removeTips() {
    let div = document.getElementById('tips');
    if (div) {
      div.style.display = 'none'
      div.innerHTML = ''
    }
  }
}


export default class Arrow {
  constructor(basicCesium) {
    this.basicCesium = basicCesium
    this.viewer = basicCesium.viewer;

    this.cesiumContainer = document.querySelector('#cesium');
    this.drawArr = [];
    this.handler = null;
    this.currentEntityId = null;
    this.isAddData = false;
    this.bindEdit();
    this.initTips();
  }

  draw(type, color) {
    if (!type) return false;
    let whichArrow;
    switch (type) {
      case 'straightArrow':
        this.addTips('左键点击开始绘制');
        whichArrow = new StraightArrow(this.basicCesium, color);
        break;
      case 'attackArrow':
        this.addTips('左键点击开始绘制');
        whichArrow = new AttackArrow(this.basicCesium, color);
        break;
      case 'pincerArrow':
        this.addTips('左键点击开始绘制');
        whichArrow = new PincerArrow(this.basicCesium, color);
        break;
    }
    if (!whichArrow) return false;
    whichArrow.startDraw();
    this.basicCesium.mouseStyle();
    this.drawArr.push(whichArrow);
  }

  loadUi(position) {
    let x = position.x + 6;
    let y = position.y + 6;
    let divs = document.querySelectorAll('.context-menu');
    if (divs != undefined && divs.length != 0) {
      this.cesiumContainer.removeChild(divs[0])
    }
    let array = [{
      text: '删除',
      type: 'del'
    }];
    let str = this.renderMenu(array)
    let div = document.createElement('div')
    div.className = 'context-menu'
    div.style.top = y + 'px'
    div.style.left = x + 'px'
    div.style.position = 'fixed'
    div.innerHTML = `<ul class="menu-list">${str}</ul>`
    this.cesiumContainer.append(div);
  }

  renderMenu(array) {
    let list = []
    window.rightClick = this.rightClick.bind(this);
    for (let i = 0; i < array.length; i++) {
      if (array[i].children) {
        list.push(`<li class="menu-list-item has-child"><div class="event">${array[i].text}</div>${ul}`)
        let children = array[i].children;
        for (let k = 0; k < children.length; k++) {
          list.push(`<li class="menu-list-item last-leaf"><div class="event" onclick="rightClick('${children[k].type}')">${children[k].text}</div></li>`)
        }
        list.push('</ul></li>')
      } else {
        list.push(`<li class="menu-list-item no-child"><div class="event" onclick="rightClick('${array[i].type}')">${array[i].text}</div></li>`)
      }
    }
    return list.join('');
  }

  rightClick(type) {
    if (type == 'del') {
      let index;
      for (var i = 0; i < this.drawArr.length; i++) {
        if (this.currentEntityId == this.drawArr[i].objId) {
          index = i;
          this.drawArr[i].clear();
          break;
        }
      }
      this.drawArr.splice(index, 1)
      this.currentEntityId = null;
      this.removeMenu();
    }
    this.removeTips();
  }

  initTips() {
    let div = document.createElement('div');
    div.id = 'tips';
    this.cesiumContainer.append(div);
  }

  changeTipsPosition(position) {
    let div = document.getElementById('tips');
    div.style.left = position.x + 15 + 'px';
    div.style.top = position.y + 'px';
    div.style.display = 'block';
  }

  addTips(text = '', ) {
    let div = document.getElementById('tips');
    div.style.display = 'none';
    div.innerHTML = `<div>${text}</div>`
  }

  removeTips() {
    let div = document.getElementById('tips');
    if (div) {
      div.style.display = 'none'
      div.innerHTML = ''
    }
  }

  bindEdit() {
    let viewer = this.viewer;
    let that = this;
    this.handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
    this.handler.setInputAction(function (e) { //单击编辑
      let pick = that.basicCesium.mousePicked(e.position);
      if (pick != undefined) {
        for (var i = 0; i < that.drawArr.length; i++) {
          if (pick.id.objId == that.drawArr[i].objId) {
            that.drawArr[i].startModify();
            break;
          }
        }
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);

    this.handler.setInputAction(function (e) { //单击编辑
      let pick = that.basicCesium.mousePicked(e.position);
      if (pick != undefined) {
        for (var i = 0; i < that.drawArr.length; i++) {
          if (pick.id.objId == that.drawArr[i].objId) {
            that.currentEntityId = pick.id.objId;
            that.loadUi(e.position);
            break;
          }
        }
      }
    }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);

    this.handler.setInputAction((e) => {
      this.removeMenu();
    }, Cesium.ScreenSpaceEventType.LEFT_DOWN);

    this.handler.setInputAction((e) => {
      let pick = this.basicCesium.mousePicked(e.endPosition);
      if (pick != undefined) {
        for (let i = 0; i < this.drawArr.length; i++) {
          if (pick.id.objId == this.drawArr[i].objId) {
            let res = this.drawArr.every(d => d.state == -1)
            if (res) {
              that.addTips('左键点击可编辑，右键点击可删除');
              that.changeTipsPosition(e.endPosition);
            }
          }
        }

      } else {
        let res = this.drawArr.every(d => d.state == -1)
        if (res) {
          that.removeTips();
        }
      }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  }

  removeMenu() {
    let divs = document.querySelectorAll('.context-menu');
    if (divs != undefined && divs.length != 0) {
      this.cesiumContainer.removeChild(divs[0])
    }
  }

  //加载数据
  showEntity(data) {
    if (!data) return false;
    if (this.isAddData) return false;
    let jsonData;
    if (typeof data == 'string') jsonData = JSON.parse(data)
    jsonData = data;
    let straightArrowArr = jsonData.straightArrowData;
    let attackArrowArr = jsonData.attackArrowData;
    let pincerArrowArr = jsonData.pincerArrowData;
    //展示直线箭头
    for (let i = 0; i < straightArrowArr.length; i++) {
      let item = straightArrowArr[i];
      let straightArrow = new StraightArrow(this.viewer, item[1]);
      straightArrow.createByData(item[0]);
      this.drawArr.push(straightArrow);
    }
    //展示攻击箭头
    for (let j = 0; j < attackArrowArr.length; j++) {
      let item = attackArrowArr[j];
      let attackArrow = new AttackArrow(this.viewer, item[1]);
      attackArrow.createByData(item[0]);
      this.drawArr.push(attackArrow);
    }
    //展示钳击箭头
    for (let z = 0; z < pincerArrowArr.length; z++) {
      let item = pincerArrowArr[z];
      let pincerArrow = new PincerArrow(this.viewer, item[1]);
      pincerArrow.createByData(item[0]);
      this.drawArr.push(pincerArrow);
    }
    this.isAddData = true;
  }
  //保存数据
  saveData() {
    var jsonData = {
      straightArrowData: [],
      attackArrowData: [],
      pincerArrowData: []
    }
    for (var i = 0; i < this.drawArr.length; i++) {
      var item = this.drawArr[i];
      if (item.type == "StraightArrow") {
        jsonData.straightArrowData.push([item.positions, item.color]);
      } else if (item.type == "AttackArrow") {
        jsonData.attackArrowData.push([item.positions, item.color]);
      } else {
        jsonData.pincerArrowData.push([item.positions, item.color]);
      }
    }
    ElMessage.success(JSON.stringify(jsonData))
  }
  //清空所有
  clearAll() {
    for (let i = 0; i < this.drawArr.length; i++) {
      this.drawArr[i].clear();
    }
    this.drawArr = [];
  }
}